package search;

import mysql.JdbcUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class SearchMQL {
    public SearchMQL(){}

    JdbcUtil jdbc = new JdbcUtil();
    /***
     * 运行sql语句并返回数据集
     * @param sql 输出SQL
     * @return 返回菜谱集
     * @throws SQLException 抛出异常
     *
     */
    public List<Recipe> mysqlSearch(String sql) throws SQLException {
        List<Recipe> recipes_rq = new ArrayList<>();
        Connection conn = jdbc.connectMySQL();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            Recipe recipe = new Recipe();
            recipe.setId(rs.getInt("recipe_id"));
            recipe.setCid(rs.getInt("category_id"));
            recipe.setName(rs.getString("recipe_name"));
            recipe.setContent(rs.getString("recipe_content"));
            recipe.setImage(rs.getString("recipe_image"));
            recipe.setCategory(rs.getString("recipe_category"));
            recipe.setTime(rs.getString("recipe_time"));
            recipe.setDescription(rs.getString("thumbnail"));
            recipe.setUpdate(rs.getString("update_date"));
            recipes_rq.add(recipe);
        }
        return recipes_rq;
    }
    public List<User> mysqlUserSearch(String sql) throws SQLException {
        List<User> users = new ArrayList<>();
        Connection conn = jdbc.connectMySQL();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            User user = new User();
            user.setId(rs.getInt("account_id"));
            user.setName(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setIco(rs.getString("ico_accounts"));
            users.add(user);
        }
        return users;
    }
}
